package com.sinya.shoppingmall.https;

import android.content.Context;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.sinya.shoppingmall.MyApplication;
import com.sinya.shoppingmall.R;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Created by Koizumi Sinya on 2015/12/15.
 */
public abstract class VolleyCallBack {

    public Context mContext;
    public static Listener<String> mSuccessListener;
    public static ErrorListener mErrorListener;

    public VolleyParams req;

    public Map<String, String> headerMap;
    public Map<String, String> paramMap;

    public int statusCode;
    public String result;
    public String error;


    public VolleyCallBack(VolleyParams req, Map<String, String> headerMap, Map<String, String> paramMap, Listener<String> succeesListener, ErrorListener errorListener) {
        this.mContext = MyApplication.getContext();
        this.req = req;

        this.headerMap = headerMap;
        commonHeader();
        this.paramMap = paramMap;
        this.mSuccessListener = succeesListener;
        this.mErrorListener = errorListener;
    }

    //TODO 作用：封装所有请求头键值对参数的Map集合
    private void commonHeader() {
        Map<String, String> newHeaderMap = new HashMap<>();
        Set<String> key = headerMap.keySet();

        //所以请求头的键已经封装到arrays文件中。Arrays.asList()：可以将可变参数形式的字符串数组转换成List集合
        for (String str : Arrays.asList(mContext.getResources().getStringArray(R.array.http_headers))) {
            newHeaderMap.put(str, "");

            for (String k : key) {
                if (str.equals(k)) {
                    newHeaderMap.put(str, headerMap.get(k));
                }
            }
        }

        //验证数据
        Set<String> newKey = newHeaderMap.keySet();
        for (String str : newKey) {
            //LogUtils.I("Sinya", "固定的请求头参数 NewHeaderMap (" + str + ", " + newHeaderMap.get(str) + ")");
        }


    //将接口回调的方法封装到VolleyCallBack类中，当外界调用的时候，可以获取接口回调的对象，但是前提是该子类对象必须实现抽象方法，以便处理返回的结果
        headerMap = newHeaderMap;
    }

    public Listener<String> getSuccessListener() {
        mSuccessListener = new Listener<String>() {
            @Override
            public void onResponse(String response) {
                setSuccessResult(statusCode, response, error);
            }
        };
        return mSuccessListener;
    }

    public ErrorListener getErrorListener() {
        mErrorListener = new ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

                setErrorResult(statusCode, result, error);
            }
        };
        return mErrorListener;
    }


    // [+] Abstract 由实现类去完成--VolleyCallBack的子类

    public abstract void setSuccessResult(int statusCode, String response, String error);

    public abstract void setErrorResult(int statusCode, String response, String error);

    // [-] Abstract
}
